using System;
using System.Threading;

namespace ebcpatterns.concurrent
{
    public class Synchronizer
    {
        private readonly SynchronizationContext synchronizationContext;

        public Synchronizer() {
            synchronizationContext = SynchronizationContext.Current ?? new SynchronizationContext();
        }

        public void Process() {
            synchronizationContext.Send(state => Result(), null);
        }

        public event Action Result;
    }

    public class Synchronizer<T>
    {
        private readonly SynchronizationContext synchronizationContext;

        public Synchronizer() {
            synchronizationContext = SynchronizationContext.Current ?? new SynchronizationContext();
        }

        public void Process(T t) {
            synchronizationContext.Send(state => Result(t), null);
        }

        public event Action<T> Result;
    }
}